/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.insurance.cis.controllers;

import com.insurance.cis.daos.CampaignDao;
import com.insurance.cis.daos.ChannelDao;
import com.insurance.cis.daos.ProductDao;
import com.insurance.cis.daos.PromotionDao;
import com.insurance.cis.models.Campaign;
import com.insurance.cis.models.Channel;
import com.insurance.cis.models.Product;
import com.insurance.cis.models.Promotion;
import com.insurance.cis.utils.CisConstants;
import com.insurance.cis.utils.MessageDialogUtils;
import com.insurance.cis.utils.Utils;
import com.insurance.cis.views.CampaignJDialogCreationView;
import java.text.ParseException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author TruongLQ
 */
public class CampaignCreationController {

    final CampaignJDialogCreationView dialogCreationView;
    final Pattern pattern = Pattern.compile(CisConstants.DATE_PATTERN_COMPILE);
    CampaignDao campaignDao = new CampaignDao();
    ProductDao productDao = new ProductDao();
    ChannelDao channelDao = new ChannelDao();
    PromotionDao promotionDao = new PromotionDao();
    
    public CampaignCreationController(CampaignJDialogCreationView aThis) {
        dialogCreationView = aThis;
    }
    public Campaign validateCampaignInfos() {
        String campaignID = dialogCreationView.getTxtCampaignID().getText().trim();
        if (campaignID.isEmpty()) {
            MessageDialogUtils.dialogError(
                    dialogCreationView, "Campaign ID must not be blank!");
            dialogCreationView.getTxtCampaignID().requestFocus();
            return null;
        }  else {
            if (campaignID.length()>20) {
                MessageDialogUtils.dialogError(
                    dialogCreationView, "Campaign ID must be maximum of 20 characters!");
                dialogCreationView.getTxtCampaignID().requestFocus();
                return null;
            }
            if (campaignDao.checkIDExist(campaignID)) {
                MessageDialogUtils.dialogError(dialogCreationView, 
                        "Campaign ID aleady exists! Please choose another ID");
                dialogCreationView.getTxtCampaignID().requestFocus();
                return null;
            }
        }
        
        String channelID = dialogCreationView.getTxtChannelID().getText().trim();
        if (channelID.isEmpty()) {
            MessageDialogUtils.dialogError(
                    dialogCreationView, "Channel ID must not be blank!");
            dialogCreationView.getTxtChannelID().requestFocus();
            return null;
        } 
        
        try {
            Integer.parseInt(channelID);
        } catch (NumberFormatException ex) {
            MessageDialogUtils.dialogError(
                    dialogCreationView, "Channel ID must be numeric!");
            dialogCreationView.getTxtChannelID().requestFocus();
            return null;
        }
        if (!channelDao.checkIDExist(channelID)) {
            MessageDialogUtils.dialogError(dialogCreationView, 
                    "No channel found with ID: " + channelID + "! Please choose another ID");
            dialogCreationView.getTxtChannelID().requestFocus();
            return null;
        }
        String campaignName = dialogCreationView.getTxtCampaignName().getText().trim();
        if (campaignName.isEmpty()) {
            MessageDialogUtils.dialogError(
                    dialogCreationView, "Campaign Name must not be blank!");
            dialogCreationView.getTxtCampaignName().requestFocus();
            return null;
        } else {
            if (campaignName.length()>50) {
                MessageDialogUtils.dialogError(
                    dialogCreationView, "Campaign Name must be maximum of 50 characters!");
                dialogCreationView.getTxtCampaignName().requestFocus();
                return null;
            }
            if (campaignDao.checkNameExist(campaignName)) {
                MessageDialogUtils.dialogError(
                    dialogCreationView, "Campaign Name aleady exists! Please choose another name");
                dialogCreationView.getTxtCampaignName().requestFocus();
                return null;
            }
        }
        
        String promotionID = dialogCreationView.getTxtPromotionID().getText().trim();
        if (promotionID.isEmpty()) {
            MessageDialogUtils.dialogError(
                    dialogCreationView, "Promotion ID must not be blank!");
            dialogCreationView.getTxtPromotionID().requestFocus();
            return null;
        } 
        
        try {
            Integer.parseInt(promotionID);
        } catch (NumberFormatException ex) {
            MessageDialogUtils.dialogError(
                    dialogCreationView, "Promotion ID must be numeric!");
            dialogCreationView.getTxtPromotionID().requestFocus();
            return null;
        }
        
        if (!promotionDao.checkIDExist(promotionID)) {
            MessageDialogUtils.dialogError(dialogCreationView, 
                    "No promotion found with ID: " + promotionID + "! Please choose another ID");
            dialogCreationView.getTxtPromotionID().requestFocus();
            return null;
        }
        
        Date startDate;
        try {
            startDate= validateDate(dialogCreationView.getTxtStartDate().getText().trim());
            if (startDate!=null) {
                // start date must be today or after today
                Date today = new Date();
                if (!dialogCreationView.getTxtStartDate().getText().trim().equals(Utils.convertDateToString(
                        today, CisConstants.DATE_DD_MM_YYYY)) &&
                        !startDate.after(today)) {
                    MessageDialogUtils.dialogError(dialogCreationView, "Start date must be today or after today!");
                    dialogCreationView.getTxtStartDate().requestFocus();
                    return null;
                }
            }
        } catch (ParseException ex) {
            MessageDialogUtils.dialogError(dialogCreationView, "Start date must be in format dd/mm/yyyy!");
            dialogCreationView.getTxtStartDate().requestFocus();
            return null;
        }
        Date endDate;
        try {
            endDate= validateDate(dialogCreationView.getTxtEndDate().getText().trim());
        } catch (ParseException ex) {
            MessageDialogUtils.dialogError(dialogCreationView, "End date must be in format dd/mm/yyyy!");
            dialogCreationView.getTxtEndDate().requestFocus();
            return null;
        }
        
        if (startDate!=null && endDate !=null && startDate.after(endDate)) {
            MessageDialogUtils.dialogError(dialogCreationView, "Start date must be <= end date!");
            dialogCreationView.getTxtStartDate().requestFocus();
            return null;
        }
        
        String productID = dialogCreationView.getTxtProductID().getText().trim();
        if (productID.isEmpty()) {
            MessageDialogUtils.dialogError(
                    dialogCreationView, "Product ID must not be blank!");
            dialogCreationView.getTxtProductID().requestFocus();
            return null;
        } else {
            if (!productDao.checkIDExist(productID)) {
                MessageDialogUtils.dialogError(dialogCreationView, 
                        "No product found with ID: " + productID + "! Please choose another ID");
                dialogCreationView.getTxtProductID().requestFocus();
                return null;
            }
        }
        
        Campaign campaign = new Campaign(campaignID, campaignName, 
                0, 0, 0, 0, 
                startDate, endDate);
        campaign.setChannel(new Channel(Integer.parseInt(channelID), ""));
        campaign.setPromotion(
                new Promotion(Integer.parseInt(promotionID), 
                0, ""));
        campaign.setProduct(new Product(productID, "", "", null));
        return campaign;
    }
    
    private Date validateDate(String date) throws ParseException {
        Date dateRet = null;

        if (!date.isEmpty()) {
            if (date.trim().length()!=10) {
                throw new ParseException("Date parse exception", 0);
            }
            Matcher matcher = pattern.matcher(date.trim());
            if (!matcher.matches()) {
                throw new ParseException("Date parse exception", 0);
            }
            dateRet = Utils.parseDate(date.trim(),
                    CisConstants.DATE_DD_MM_YYYY);
        }
        return dateRet;
    }
}
